package com.lalilu.homework3.fragment;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

public class MultiChoiceDialogFragment extends DialogFragment {
    private final String[] genders = new String[]{"男", "女"};
    private final Set<String> selectItem = new LinkedHashSet<>();
    private NotifyDialogFragment listener;

    public void setListener(NotifyDialogFragment listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new AlertDialog.Builder(Objects.requireNonNull(getContext()))
                .setTitle("多选对话框")
                .setMultiChoiceItems(genders, null, (dialog, which, isChecked) -> {
                    if (isChecked) {
                        selectItem.add(genders[which]);
                    } else {
                        selectItem.remove(genders[which]);
                    }
                })
                .setPositiveButton("确定", (dialog, which) -> this.listener.onDialogPositiveClicked(selectItem.toString()))
                .setNegativeButton("取消", (dialog, which) -> dialog.cancel())
                .create();
    }
}
